package utils;

import javax.swing.JEditorPane;
import javax.swing.JOptionPane;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;


public class GoogleMapEditorPan extends JEditorPane{
	private static final long serialVersionUID = 1L;
    public Document dom;
    
    /**
     * Constructeur: initialisation du EditorKit
     */
    public GoogleMapEditorPan() {
        HTMLEditorKit kit = new HTMLEditorKit();
        HTMLDocument htmlDoc = (HTMLDocument) kit.createDefaultDocument();
        setEditable(false);
        setContentType("text/html");
        setEditorKit(kit);
        setDocument(htmlDoc);
    }
    
    public String setDirection(String emplacement, String addressRest, String addressTo) throws Exception {
    	
    	String polyLine = "";
    	
    	try{
	    	String directions = "http://maps.google.com/maps/api/directions/xml?";
	    	directions += "&origin=" + URLEncoder.encode(emplacement,"UTF-8");
	        directions += "&waypoints=" + URLEncoder.encode(addressRest,"UTF-8");
	        directions += "&destination=" + URLEncoder.encode(addressTo,"UTF-8");
	        directions += "&sensor=false";
	        
	        	System.out.println("*******************URL DIRECTION******************************");
	        	System.out.println(directions);
	        	
	        	URL googleMap = new URL(directions);
	        	URLConnection urlConnection = googleMap.openConnection();
	        	
	        	BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
	    			
	        	String inputLine;
	        	System.out.println("*******************XML INFORMATION******************************");	
	    			while ((inputLine = in.readLine()) != null){
	    				System.out.println(inputLine);
	    				if(inputLine.trim().equals("<overview_polyline>")){
	    					polyLine = in.readLine();
	    					System.out.println(polyLine);
	    				}
	    				if(inputLine.trim().equals("<status>ZERO_RESULTS</status>")){
	    					polyLine = "";
	    					throw new Exception();
	    				}
	    			}
	    			in.close();
    	}catch(IOException e){
    		System.out.println("Failed Connection to server, Please verify your internet connection");
    		JOptionPane.showMessageDialog(this, "Failed Connection to server, Please verify your internet connection");
    	}catch(Exception e){
    		System.out.println("No result found, please re enter your location with more precision");
    		JOptionPane.showMessageDialog(this, "No result found");
    	}
    	
        return polyLine;
    }
    
    
    public void setMap(String emplacement, String addressRest, String addressTo, Integer width, Integer height) throws Exception {
    	
    	String polyLineData;

    	polyLineData = setDirection(emplacement, addressRest, addressTo);
    	
    	polyLineData = polyLineData.replaceAll("<points>", "");
    	polyLineData = polyLineData.replaceAll("</points>", "");
    	polyLineData = polyLineData.trim();
    	
    	System.out.println("****************POLYLINE*************************");
    	System.out.println(polyLineData);
    	System.out.println("*************************************************");
    	
    	String url = "http://maps.google.com/maps/api/staticmap?";
        url += "size=" + width + "x" + height;        
        url += "&markers=color:green|size:mid|" + URLEncoder.encode(emplacement,"UTF-8") + ",quebec,canada" + 
        	"|" + URLEncoder.encode(addressRest,"UTF-8") + "|" + URLEncoder.encode(addressTo,"UTF-8") + "|" ;
        url += "&path=color:0x0000ff|weight:5|enc:"+ polyLineData;        
        url += "&sensor=false";
        
        System.out.println("*******************URL DIRECTION******************************");
        System.out.println(url);
        
        String html = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>";
        html += "<html><head></head><body>";
        html += "<img src='" + url + "'>";
        html += "</body></html>";
        
        if(polyLineData.equals("") == false)
        	setText(html);
    }

}